package com.bjpowernode.springboot.domain;

import java.util.Iterator;
import java.util.List;

/**
 * Copyright (C), 2021-2021, 颜子初
 * Author: yan
 * Date: 2021/11/12 16:56
 * FileName: Students
 * Description:
 */
public class Students implements Iterable<Student>{
    private Student[] students;

    public Students(List<Student> students){

        this.students = new Student[students.size()];
        for(int i = 0; i < students.size(); i++){
            this.students[i] = students.get(i);
        }
    }

    @Override
    public Iterator<Student> iterator() {
        return new StudentIterator();
    }

    // 实现Iterator接口的私有内部类，外界无法直接访问
    private class StudentIterator implements Iterator<Student> {
        // 当前迭代元素的下标
        private int index = 0;

        // 判断是否还有下一个元素，如果迭代到最后一个元素就返回false
        @Override
        public boolean hasNext() {
            return index != students.length;
        }

        @Override
        public Student next() {
            return students[index++];
        }

        // 这里不支持，抛出不支持操作异常
        @Override
        public void remove() {
            throw new UnsupportedOperationException();
        }
    }

}
